package it.polimi.travelDream.ejb.beans;

import java.util.List;

import it.polimi.travelDream.ejb.interfaces.TravelListMgrRemote;
import it.polimi.travelDream.entities.Hotel;
import it.polimi.travelDream.entities.Modified_Package;
import it.polimi.travelDream.entities.Package;
import it.polimi.travelDream.entities.TravelList;
import it.polimi.travelDream.ejb.Cthulhu.*;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

/**
 * Session Bean implementation class TravelListMgr
 */
@Stateless
@LocalBean
public class TravelListMgr implements TravelListMgrRemote {
	
	Persistenza persist=new Persistenza();
	public EntityManagerFactory factory;
	public EntityManager em;
	
	TravelList travel;
    public TravelList getTravel() {
		return travel;
	}

	public void setTravel(TravelList travel) {
		this.travel = travel;
	}
    /**
     * Default constructor. 
     */
    public TravelListMgr() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public void addToList() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delByList() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void modifyTL() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void joinTravel() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public TravelList add(TravelList tl){
		persist.persista(tl);
		return tl;
	}
	
	@Override
	public List<TravelList> ShowAllTravelList(int userid){
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();
		
		TypedQuery<TravelList> query = em.createQuery("SELECT tl FROM TravelList tl WHERE tl.idUser="+userid+"", TravelList.class);

			List<TravelList> travellist = query.getResultList();

			em.close();
			factory.close();
			return travellist;
	}

	@Override
	public void delTravelList(int id){
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();
		TravelList tl = em.find(TravelList.class, id);
		em.getTransaction().begin();
		em.remove(tl);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}
	
}
